from sqlalchemy.future import select
from sqlalchemy.ext.asyncio import AsyncSession
from ..configuration.DataBaseConfiguration  import get_session
from fastapi import Depends
from ..models import User

class UserMapper:
    def __init__(self, session: AsyncSession):
        self.session = session

    async def get_user_by_id(self, user_id: int) -> "User | None":
        result = await self.session.execute(select(User).where(User.id == user_id))
        return result.scalars().first()

    async def create_user(self, username: str, password: str, role: str = "admin") -> User:
        new_user = User(username=username, password=password, role=role)
        self.session.add(new_user)
        await self.session.commit()
        await self.session.refresh(new_user)
        return new_user

    async def update_user_password(self, user_id: int, new_password: str) -> bool:
        user = await self.get_user_by_id(user_id)
        if not user:
            return False
        user.password = new_password
        self.session.add(user)
        await self.session.commit()
        return True

    async def delete_user(self, user_id: int) -> bool:
        user = await self.get_user_by_id(user_id)
        if not user:
            return False
        await self.session.delete(user)
        await self.session.commit()
        return True
